package kr.ac.chungbuk;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Servlet implementation class EmpSelectServlet
 */
@WebServlet("/EmpSelect")
public class EmpSelectServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=EUC-KR");
		PrintWriter out = response.getWriter();
		out.print("<html><body>");
		
		String driver = "oracle.jdbc.driver.OracleDriver";
		String url = "jdbc:oracle:thin:@127.0.0.1:1521:XE";
		String userid = "KMS";
		String passwd = "dkdlxl";
		
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try{
			Class.forName(driver);
			con = DriverManager.getConnection(url, userid, passwd);
			stmt = con.createStatement();
			String sql = "select emp_id, ename, salary, depart from emp";
			rs = stmt.executeQuery(sql);
			
			while(rs.next())
			{
				String emp_id = rs.getString("emp_id");
				String ename= rs.getString("ename");
				int salary = rs.getInt("salary");
				String depart = rs.getString("depart");
				
				out.print(emp_id+"\t"+ename+"\t"+salary+"\t"+depart+"\t"+"<br>");
			}
			
		} catch(Exception e){
			e.printStackTrace();
		} finally{
			try{
				if(rs!=null) rs.close();
				if(stmt!=null) stmt.close();
				if(con!=null) con.close();
			} catch(SQLException e){
				e.printStackTrace();
			}
		}
		
		out.print("</body></html>");
		
	}

}
